package com.xyzq.webapp.action.sm;

import java.util.Date;
import java.util.List;

import org.appfuse.model.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.opensymphony.xwork2.Preparable;
import com.xyzq.model.common.DataDictionary;
import com.xyzq.model.sm.OrderClient;
import com.xyzq.model.ya.data.Account;
import com.xyzq.service.common.DataDictionaryManager;
import com.xyzq.service.sm.OrderClientManager;
import com.xyzq.service.ya.AccountManager;
import com.xyzq.webapp.action.BaseAction;

public class OrderClientAction extends BaseAction implements Preparable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1744300520643674856L;
	private OrderClientManager orderClientManager;
	private DataDictionaryManager dataDictionaryManager;
	private AccountManager accountManager;
	private List<OrderClient> orderClients;
	private List<DataDictionary> orderSources;
	private List<DataDictionary> orderTypes;
	private List<Account> accountList;
	private List<OrderClient> conflictOrderClientList;
	private OrderClient orderClient;
	private Long id;
	private boolean subSystem;

	public void setOrderClients(List<OrderClient> orderClients) {
		this.orderClients = orderClients;
	}

	public void setOrderSources(List<DataDictionary> orderSources) {
		this.orderSources = orderSources;
	}

	public void setOrderTypes(List<DataDictionary> orderTypes) {
		this.orderTypes = orderTypes;
	}

	public void prepare() throws Exception {
		this.orderSources = this.getDataDictionaryManager().findByModule(
				"orderClientSource");
		this.orderTypes = this.getDataDictionaryManager().findByModule(
				"orderClientType");
	}

	public String list() {
		this.orderClients = this.getOrderClientManager().getAll();
		this.logUserAction("orderClientAction", "read all list");
		return SUCCESS;
	}

	public String userList() {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		User user = (User) auth.getPrincipal();
		this.orderClients = this.getOrderClientManager().findByUser(user);
		this.logUserAction("orderClientAction", "read self list");
		return SUCCESS;
	}

	public String delete() {
		getOrderClientManager().remove(orderClient.getId());
		saveMessage(getText("orderClient.deleted"));
		this.logUserAction("orderClientAction",
				"delete:" + this.getOrderClient());
		return SUCCESS;
	}

	public String edit() {
		if (getId() != null) {
			orderClient = getOrderClientManager().get(getId());
			Authentication auth = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auth.getPrincipal();
			if (!user.getId().equals(orderClient.getUser().getId())) {
				orderClient = new OrderClient();
			}
			this.logUserAction("orderClientAction",
					"ready edit:" + this.getOrderClient());
		} else {
			orderClient = new OrderClient();
			this.logUserAction("orderClientAction",
					"ready input:" + this.getOrderClient());
		}
		this.subSystem = false;
		return SUCCESS;
	}

	public String editSubSystem() {
		if (getId() != null) {
			this.orderClient = getOrderClientManager().get(getId());
			this.conflictOrderClientList = this.orderClientManager
					.findByConflictOrderClient(orderClient);
			Integer idCard=null;
			if (this.orderClient.getIdCard()!=null&&this.orderClient.getIdCard().length()>0) {
				 idCard = Integer.parseInt(this.orderClient.getIdCard());
			}
			this.accountList = this.accountManager.findByAccountOrName(idCard,
					this.orderClient.getClient());
			this.subSystem = true;
			this.logUserAction("orderClientAction",
					"ready edit:" + this.getOrderClient());
		} else {
			orderClient = new OrderClient();
			this.subSystem = false;
			this.logUserAction("orderClientAction",
					"ready input:" + this.getOrderClient());
		}
		return SUCCESS;
	}

	public String save() throws Exception {
		if (cancel != null) {
			return CANCEL;
		}
		if (delete != null) {
			return delete();
		}
		boolean isNew = (orderClient.getId() == null);
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		User user = (User) auth.getPrincipal();
		if (isNew) {
			orderClient.setUser(user);
			orderClient.setSubTime(new Date());
		} else {
			OrderClient oc = getOrderClientManager().get(
					this.orderClient.getId());
			orderClient.setUser(oc.getUser());
			orderClient.setSubTime(oc.getSubTime());
			orderClient.setModifyTime(new Date());
		}
		getOrderClientManager().save(orderClient);
		String key = (isNew) ? "orderClient.added" : "orderClient.updated";
		saveMessage(getText(key));
		if (!isNew) {
			this.logUserAction("orderClientAction",
					"edit:" + this.getOrderClient());
			return INPUT;
		} else {
			this.logUserAction("orderClientAction",
					"save:" + this.getOrderClient());
			return SUCCESS;
		}
	}

	public void setOrderClient(OrderClient orderClient) {
		this.orderClient = orderClient;
	}

	public OrderClient getOrderClient() {
		return orderClient;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public List<OrderClient> getOrderClients() {
		return orderClients;
	}

	public List<DataDictionary> getOrderSources() {
		return orderSources;
	}

	public List<DataDictionary> getOrderTypes() {
		return orderTypes;
	}

	public void setOrderClientManager(OrderClientManager orderClientManager) {
		this.orderClientManager = orderClientManager;
	}

	public OrderClientManager getOrderClientManager() {
		return orderClientManager;
	}

	public void setDataDictionaryManager(
			DataDictionaryManager dataDictionaryManager) {
		this.dataDictionaryManager = dataDictionaryManager;
	}

	public DataDictionaryManager getDataDictionaryManager() {
		return dataDictionaryManager;
	}

	public boolean isSubSystem() {
		return subSystem;
	}

	public void setSubSystem(boolean subSystem) {
		this.subSystem = subSystem;
	}

	public List<Account> getAccountList() {
		return accountList;
	}

	public void setAccountList(List<Account> accountList) {
		this.accountList = accountList;
	}

	public AccountManager getAccountManager() {
		return accountManager;
	}

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public List<OrderClient> getConflictOrderClientList() {
		return conflictOrderClientList;
	}

	public void setConflictOrderClientList(
			List<OrderClient> conflictOrderClientList) {
		this.conflictOrderClientList = conflictOrderClientList;
	}

}
